/**
 * 
 */
package tr.edu.metu.is.mega.client.course.activity;

import java.util.Date;

import tr.edu.metu.is.mega.client.core.AsyncCallbackAdapter;
import tr.edu.metu.is.mega.client.core.DialogCallbackAdapter;
import tr.edu.metu.is.mega.client.core.DownloadHelper;
import tr.edu.metu.is.mega.client.core.event.SelectionEventBus;
import tr.edu.metu.is.mega.client.course.CourseChangeEvent;
import tr.edu.metu.is.mega.client.course.instructor.students.ReviewerEditDialog;
import tr.edu.metu.is.mega.client.resource.MegaConstants;
import tr.edu.metu.is.mega.client.service.GwtCourseService;
import tr.edu.metu.is.mega.client.service.GwtSubmissionViewService;
import tr.edu.metu.is.mega.common.dto.ReviewerDTO;
import tr.edu.metu.is.mega.common.dto.SubmissionDTO;
import tr.edu.metu.is.mega.common.dto.SubmissionViewDTO;
import tr.edu.metu.is.mega.common.dto.UserDTO;
import tr.edu.metu.is.mega.common.dto.UserRole;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;

/**
 * Helper container class for {@link ActivityManagementPanel} toolbar buttons.
 */
public final class ActivityManagementHelpers {

    /**
     * Default constructor.
     */
    private ActivityManagementHelpers() {
    }

    /**
     * Button action helper of {@link ActivityManagementPanel} toolbar buttons.
     * 
     * @param course selected course
     * @param team selected team
     * @param role user role
     * @param grid grid in panel
     * @return selection listener for button
     */
    public static SelectionListener<ButtonEvent> getAssignReviewerAction(final ActivityManagementPanel panel) {
        return new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                if (panel.getCourse() != null && panel.getTeam() != null && panel.getGrid().getSelectionModel().getSelectedItem() != null) {
                    SubmissionViewDTO dto = panel.getGrid().getSelectionModel().getSelectedItem().getBean();
                    ReviewerEditDialog editDialog = new ReviewerEditDialog(panel.getCourse(), dto.getActivityid());
                    editDialog.create(new ReviewerDTO(), new DialogCallbackAdapter<ReviewerDTO>() {

                        @Override
                        public void onOk(ReviewerDTO param) {
                            if (panel.getTeam().getPkid() == param.getReviewerTeam().getPkid()) {
                                Info.display("Warning", "Review team and selected team cannot be the same!");
                                return;
                            }
                            param.setTeam(panel.getTeam());
                            GwtSubmissionViewService.Util.getInstance().assignReviewer(param, new AsyncCallbackAdapter<Void>() {

                                @Override
                                public void onSuccess(Void result) {
                                    SelectionEventBus.getInstance().fireEvent(CourseChangeEvent.TEAMSELECTED, panel.getTeam());
                                }
                            });
                        }
                    });

                }
            }
        };
    }

    /**
     * /** Button action helper of {@link ActivityManagementPanel} toolbar
     * buttons.
     * 
     * @param panel reference
     * @return selection listener for button
     */
    public static SelectionListener<ButtonEvent> getDownloadSubmissionAction(final ActivityManagementPanel panel) {
        return new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                if (panel.getGrid().getSelectionModel().getSelectedItem() != null) {
                    SubmissionViewDTO dto = panel.getGrid().getSelectionModel().getSelectedItem().getBean();
                    if (dto.getSubmissionfile() != null) {
                        DownloadHelper.downloadFile(MegaConstants.TEAM_PATH + dto.getTeamid() + "/submission/" + dto.getSubmissionfile());
                    } else {
                        Info.display("Warning", "No submission submitted yet!");
                    }
                }
            }
        };
    }

    /**
     * /** Button action helper of {@link ActivityManagementPanel} toolbar
     * buttons.
     * 
     * @param panel reference
     * @return selection listener for button
     */
    public static SelectionListener<ButtonEvent> getShowRevisionsAction(final ActivityManagementPanel panel) {
        return new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                if (panel.getGrid().getSelectionModel().getSelectedItem() != null) {
                    SubmissionViewDTO dto = panel.getGrid().getSelectionModel().getSelectedItem().getBean();
                    SubmissionRevisionDialog dialog = new SubmissionRevisionDialog(dto.getActivityid(), dto.getTeamid());
                    dialog.show();
                }
            }
        };
    }

    /**
     * Button action helper of {@link ActivityManagementPanel} toolbar buttons.
     * 
     * @param panel reference
     * @return selection listener for button
     */
    public static SelectionListener<ButtonEvent> getUploadSubmissionAction(final ActivityManagementPanel panel) {
        return new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                if (panel.getGrid().getSelectionModel().getSelectedItem() != null) {
                    final SubmissionViewDTO view = panel.getGrid().getSelectionModel().getSelectedItem().getBean();
                    SubmissionDTO submission = new SubmissionDTO();
                    submission.setPkid(view.getSubmissionid());
                    submission.setSubmissiondate(new Date());
                    submission.setActivity(view.getActivityid());
                    submission.setTeam(panel.getTeam());

                    final SubmissionUploadDialog dialog = new SubmissionUploadDialog(submission);
                    dialog.update(submission, new DialogCallbackAdapter<SubmissionDTO>() {

                        @Override
                        public void onOk(final SubmissionDTO submission) {
                            GwtSubmissionViewService.Util.getInstance().uploadSubmission(submission, new AsyncCallbackAdapter<Void>() {

                                @Override
                                public void onSuccess(Void result) {
                                    SelectionEventBus.getInstance().fireEvent(CourseChangeEvent.TEAMSELECTED, panel.getTeam());
                                }
                            });
                        }

                    });
                }
            }
        };
    }

    /**
     * Button action helper of {@link ActivityManagementPanel} toolbar buttons.
     * 
     * @param role user role
     * @param grid grid in panel
     * @return selection listener for button
     */
    public static SelectionListener<ButtonEvent> getDownloadReviewReportAction(final ActivityManagementPanel panel) {
        return new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                if (panel.getGrid().getSelectionModel().getSelectedItem() != null) {
                    SubmissionViewDTO dto = panel.getGrid().getSelectionModel().getSelectedItem().getBean();
                    if (dto.getReviewFile() != null) {
                        DownloadHelper.downloadFile(MegaConstants.TEAM_PATH + dto.getReviewteamid() + "/submission/" + dto.getReviewFile());
                    } else {
                        Info.display("Warning", "No review submitted yet!");
                    }
                }
            }
        };
    }

    /**
     * Button action helper of {@link ActivityManagementPanel} toolbar buttons.
     * 
     * @param role user role
     * @param grid grid in panel
     * @return selection listener for button
     */
    public static SelectionListener<ButtonEvent> getExportToGoogleCalendarAction(final ActivityManagementPanel panel) {
        return new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                ExportCalendarDialog dialog = new ExportCalendarDialog();
                dialog.create(new UserDTO(), new DialogCallbackAdapter<UserDTO>() {

                    @Override
                    public void onOk(UserDTO param) {
                        GwtCourseService.Util.getInstance().exportToGoogleCalendar(param.getEmail(), param.getPassword(), panel.getCourse(),
                                new AsyncCallbackAdapter<Boolean>() {

                                    @Override
                                    public void onSuccess(Boolean result) {
                                        if (result == true) {
                                            Info.display("Succeeded", "Activities are exported to your calendar.");
                                        }
                                    }
                                });
                    }
                });
            }
        };
    }

    /**
     * Button action helper of {@link ActivityManagementPanel} toolbar buttons.
     * 
     * @param role user role
     * @param grid grid in panel
     * @return selection listener for button
     */
    public static SelectionListener<ButtonEvent> getDownloadAllAction(final ActivityManagementPanel panel) {
        return new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                UserRole role = panel.getRole();
                int cid = panel.getCourse().getPkid();
                int tid = 0;
                try {
                    tid = panel.getTeam().getPkid();
                } catch (Exception e) {
                    // TODO: handle exception
                }
                GwtSubmissionViewService.Util.getInstance().downloadAll(role, cid, tid, new AsyncCallbackAdapter<String>() {

                    @Override
                    public void onSuccess(String result) {
                        DownloadHelper.downloadFile(result);
                    }
                });
            }
        };
    }

}
